package com.google.code.jmarias.client;

import com.google.code.jmarias.client.components.AbstractRenderComponent;
import com.google.code.jmarias.client.components.AbstractComponent;
import com.google.code.jmarias.common.Card;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Definovane rozhrani Entity.
 * Entity jsou veskere objekty ve hre. Skryte i renderovane.
 * Entity si udrzuji svoji aktualni pozici, velikost (scale) a rotaci vuci hraci plose.
 * Dale obsahuji seznam komponent ktere se zajistuji funkcionalitu entity (pohyb, textury atd.).
 *
 * ID entity je uchovavano jako int.
 * 
 * @author bart
 */
public class Entity {

    private String id;
    private Vector2f position;
    private float scale;
    private float rotation;
    private Card cardObject;
    private int cardIndex;

    private AbstractRenderComponent renderComponent = null;
    private ArrayList<AbstractComponent> components = new ArrayList<AbstractComponent>();

    public Entity(String id) {
        this.id = id;

        position = new Vector2f(0,0);
        scale = 1;
        rotation = 0;
    }

    public Entity(String id, int cardIndex, Card cardObject) {
        this.cardObject = cardObject;
        this.cardIndex = cardIndex;
    }


    /**
     * Pridani komponenty k aktualni entite.
     *
     * @param component Ukazatel na typ komponenty.
     */
    public void AddComponent(AbstractComponent component){
        if(AbstractRenderComponent.class.isInstance(component)) {
            renderComponent = (AbstractRenderComponent)component;
        }

        component.setOwnerEntity(this);
        components.add(component);
    }


    public void DelComponent(int id) {
        components.remove(id);
    }

    /**
     * Vraceni ukazatele na komponentu.
     *
     * @param id ID pozadovane komponenty
     * @return Komponentu
     */
    public AbstractComponent getComponent(int id) {
        for(int i=0; i<components.size(); i++ ) {
//        for (AbstractComponent comp : components) {
            if(components.get(i) != null) {
              if(components.get(i).getId() == id) {
                return components.get(i);
              }
            }
        }

        return null;
    }




    /**
     * @return Vrati aktualni pozici entity.
     */
    public Vector2f getPosition() {
        return position;
    }

    /**
     * @return Vrati aktualni zvetseni entity.
     */
    public float getScale(){
        return scale;
    }

    /**
     * @return Vrati aktualni rotaci komponenty vuci stolu.
     */
    public float getRotation() {
        return rotation;
    }

    /**
     * @return Vrati identifikacni cislo entity.
     */
    public String getId() {
        return id;
    }

    /**
     * Nastavi pozici entity
     * @param position Vektor souradnic ve formatu (x,y)
     */
    public void setPosition(Vector2f position) {
        this.position = position;
    }

    /**
     * Nastavi uhel rotace entity vuci stolu.
     * 
     */
    public void setRotation(float rotate) {
        this.rotation = rotate;
    }

    /**
     * Nastavi zvetseni entity
     * @param scale Meritko zvetseni
     */
    public void setScale(float scale) {
        this.scale = scale;
    }

    public Card getCardObject() {
        return cardObject;
    }

    public int getCardIndex() {
        return cardIndex;
    }

    

    

    /**
     * Update entity. (herni smycka).
     */
    public void update(GameContainer gc, StateBasedGame sb, int delta) {
        for(int i=0; i < components.size(); i++) {
//        for(AbstractComponent component : components) {
           if(components.get(i) != null)
                components.get(i).update(gc, sb, delta);
        }
    }

    /**
     * Vykresleni entity.
     */
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
        if(renderComponent != null) {
            renderComponent.render(gc,sb,gr);
        }
    }


}
